package com.aierjun.line;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.util.AttributeSet;
import android.widget.EditText;

import com.aierjun.notepadjun.R;


/**
 * Created by Administrator on 2016/6/21.
 */
public class DashLineEditText extends EditText {

    protected Paint dashLinePaint;
    private int dashLineColor= Color.GRAY;
    private int dashLineHeight=1;
    private int dashLineHeightOffset=0;

    public DashLineEditText(Context context) {
        this(context,null);
    }

    public DashLineEditText(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public DashLineEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        dashLineHeightOffset = (int)(0.19753087f * getLineHeight() + 0.5f);

        TypedArray ta=context.obtainStyledAttributes(attrs, R.styleable.DashLineEeditText);
        dashLineColor=ta.getColor(R.styleable.DashLineEeditText_dashLineColor,dashLineColor);
        dashLineHeight=ta.getDimensionPixelSize(R.styleable.DashLineEeditText_dashLineHeight,dashLineHeight);
        dashLineHeightOffset=ta.getDimensionPixelSize(R.styleable.DashLineEeditText_dashLineHeightOffset,dashLineHeightOffset);
        ta.recycle();

        dashLinePaint=new Paint();
        dashLinePaint.setStrokeWidth(dashLineHeight);
        dashLinePaint.setColor(dashLineColor);
        dashLinePaint.setStyle(Paint.Style.FILL_AND_STROKE);
        PathEffect pathEffect=new DashPathEffect(new float[]{5,5,5,5},1);
        dashLinePaint.setPathEffect(pathEffect);
    }
    protected void onDraw(Canvas canvas){
        super.onDraw(canvas);
        int lineCount=getLineCount();
        int height =getHeight();
        int lineHeight=getLineHeight();

        lineCount=Math.max(lineCount,1+height/lineHeight);
        int lineY=getCompoundPaddingTop()+dashLineHeightOffset;

        for (int i=0;i<=lineCount;i++){
            lineY+=lineHeight;
            Path path =new Path();
            path.moveTo(getCompoundPaddingLeft(),lineY);
            path.lineTo(getWidth()-getPaddingRight(),lineY);
            canvas.drawPath(path,this.dashLinePaint);
            canvas.save();
        }
        super.onDraw(canvas);
    }
}
